package com.lingyu.jsjx.service.impl;

import java.util.List;
import java.util.Map;

import com.lingyu.jsjx.domain.TeachingReflection;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyu.jsjx.mapper.ScientificResearchMapper;
import com.lingyu.jsjx.domain.ScientificResearch;
import com.lingyu.jsjx.service.IScientificResearchService;

import javax.validation.Validator;

/**
 * 科研管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@Service
public class ScientificResearchServiceImpl  extends ServiceImpl<ScientificResearchMapper, ScientificResearch> implements IScientificResearchService
{
    @Autowired
    private ScientificResearchMapper scientificResearchMapper;
    @Autowired
    private RemoteDeptService deptService;
    @Autowired
    private Validator validator;
    /**
     * 查询科研管理
     * 
     * @param researchId 科研管理主键
     * @return 科研管理
     */
    @Override
    public ScientificResearch selectScientificResearchByResearchId(Long researchId)
    {
        return scientificResearchMapper.selectScientificResearchByResearchId(researchId);
    }

    /**
     * 查询科研管理列表
     * 
     * @param scientificResearch 科研管理
     * @return 科研管理
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "sr",type = "jsjx")
    public List<ScientificResearch> selectScientificResearchList(ScientificResearch scientificResearch)
    {
        return scientificResearchMapper.selectScientificResearchList(scientificResearch);
    }

    /**
     * 新增科研管理
     * 
     * @param scientificResearch 科研管理
     * @return 结果
     */
    @Override
    public int insertScientificResearch(ScientificResearch scientificResearch)
    {
        return baseMapper.insert(scientificResearch);
    }

    /**
     * 修改科研管理
     * 
     * @param scientificResearch 科研管理
     * @return 结果
     */
    @Override
    public int updateScientificResearch(ScientificResearch scientificResearch)
    {

        return baseMapper.updateById(scientificResearch);
    }

    /**
     * 批量删除科研管理
     * 
     * @param researchIds 需要删除的科研管理主键
     * @return 结果
     */
    @Override
    public int deleteScientificResearchByResearchIds(Long[] researchIds)
    {
        return scientificResearchMapper.deleteScientificResearchByResearchIds(researchIds);
    }

    /**
     * 删除科研管理信息
     * 
     * @param researchId 科研管理主键
     * @return 结果
     */
    @Override
    public int deleteScientificResearchByResearchId(Long researchId)
    {
        return baseMapper.deleteById(researchId);
    }

    /**
     * 查询各学院科研预算和数量统计
     * @return 科研管理统计
     */
    @Override
    public List<Map<String, Object>> selectCountBudgetStatistic() {
        return scientificResearchMapper.selectCountBudgetStatistic();
    }

    /**
     * 查询各科研类型数量统计（进行中、已完成）
     * @return
     */
    @Override
    public List<Map<String, Object>> selectResearchTypeCount() {
        return scientificResearchMapper.selectResearchTypeCount();
    }

    /**
     * 导入科研管理数据
     * @param scientificResearchList
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importScientificResearch(List<ScientificResearch> scientificResearchList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(scientificResearchList) || scientificResearchList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (ScientificResearch scientificResearch : scientificResearchList) {
            try {
                SysDept dept1 = deptService.selectDeptByName(scientificResearch.getCollageName(), 0L, SecurityConstants.INNER).getData();
                if (dept1 != null) {
                    scientificResearch.setCollageId(dept1.getDeptId());
                }

                scientificResearch.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                scientificResearch.setCreateByName(SecurityUtils.getUsername());
                scientificResearch.setCreateTime(DateUtils.getNowDate());

                // 验证数据合法性
                BeanValidators.validateWithException(validator, scientificResearch);
                scientificResearchMapper.insert(scientificResearch);
                successNum++;
                successMsg.append("<br/>" + successNum + "、科研管理导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、科研管理导入失败";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}
